package cn.virens.web.components.session.resolver;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.session.web.http.HttpSessionIdResolver;

public class ParamHttpSessionIdResolver implements HttpSessionIdResolver {
	private static final String PARAM_SESSION = "SESSIONID";

	private String parameterName = PARAM_SESSION;

	public static ParamHttpSessionIdResolver newSessionIdResolver() {
		return new ParamHttpSessionIdResolver();
	}

	@Override
	public List<String> resolveSessionIds(HttpServletRequest request) {
		String[] paramSessionID = request.getParameterValues(parameterName);
		if (paramSessionID != null && paramSessionID.length > 0) {
			return Arrays.asList(paramSessionID);
		} else {
			return Collections.emptyList();
		}
	}

	@Override
	public void setSessionId(HttpServletRequest request, HttpServletResponse response, String sessionId) {
	}

	@Override
	public void expireSession(HttpServletRequest request, HttpServletResponse response) {
	}

	public String getParameterName() {
		return parameterName;
	}

	public void setParameterName(String parameterName) {
		this.parameterName = parameterName;
	}
}
